package com.codbking.calendar.exaple;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.codbking.calendar.exaple.calendar.CalendarAdapter;
import com.codbking.calendar.exaple.calendar.CalendarDateView;
import com.codbking.calendar.exaple.calendar.CalendarInfo;
import com.codbking.calendar.exaple.calendar.CalendarLayout;
import com.codbking.calendar.exaple.calendar.CalendarTextView;
import com.codbking.calendar.exaple.calendar.CalendarUtil;
import com.codbking.calendar.exaple.calendar.CalendarView;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class CalendarActivity extends AppCompatActivity {

    private static final String TAG = "CalendarActivity";
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.calendarDateView)
    CalendarDateView mCalendarDateView;
    @BindView(R.id.list)
    ListView mList;
    @BindView(R.id.layout)
    CalendarLayout linearLayout;

    private static Date date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar2);
        ButterKnife.bind(this);
        date = new Date();
        date.getTime();
        Log.d("sssssssss init", date.getTime() + "  ");
        initView();
        initList();
    }

    public  void printDate() {
        if (date!=null)
        Log.d("sssssssss", date.getTime() + "  ");
    }

    private void initList() {
        mList.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return 100;
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = LayoutInflater.from(CalendarActivity.this).inflate(android.R.layout.simple_list_item_1, null);
                }

                TextView textView = (TextView) convertView;
                textView.setText("position:" + position);
                return convertView;
            }
        });
    }

    private void initView() {

        mCalendarDateView.setAdapter(new CalendarAdapter() {
            @Override
            public View getView(View convertView, ViewGroup parentView, CalendarInfo bean, boolean isFullScreen) {

                if (convertView == null) {
                    convertView = LayoutInflater.from(parentView.getContext()).inflate(R.layout.item_xiaomi111, null);
                }
                CalendarTextView chinaText = (CalendarTextView) convertView.findViewById(R.id.chinaText);
                CalendarTextView text = (CalendarTextView) convertView.findViewById(R.id.text);
                if (convertView.isSelected()) {
                }
                text.setText("" + bean.getDay());

                if (bean.getMouthFlag() != 0) {
                    text.setTextColor(0xff9299a1);
                } else {
                    text.setTextColor(0xff444444);
                }
                chinaText.setText(bean.getChinaDay());
                CalendarTextView one = (CalendarTextView) convertView.findViewById(R.id.one);
                CalendarTextView two = (CalendarTextView) convertView.findViewById(R.id.two);
                CalendarTextView three = (CalendarTextView) convertView.findViewById(R.id.three);
                one.setText("1111");
                two.setText("222");
                three.setText("333");
                if (!isFullScreen) {
                    one.changeAlpha(0);
                    two.changeAlpha(0);
                    three.changeAlpha(0);
                } else {
                    one.changeAlpha(138);
                    two.changeAlpha(138);
                    three.changeAlpha(138);
                }
                return convertView;
            }

            @Override
            public void setAnimationWhenItemViewScroll(View childView, float scrollDistanceScale) {
                Log.d(TAG, "alpha : " + scrollDistanceScale + "   " + scrollDistanceScale);
                CalendarTextView one = (CalendarTextView) childView.findViewById(R.id.one);
                CalendarTextView two = (CalendarTextView) childView.findViewById(R.id.two);
                CalendarTextView three = (CalendarTextView) childView.findViewById(R.id.three);
                int alpha = (int) (138 * (1 - scrollDistanceScale));
                one.changeAlpha(alpha);
                two.changeAlpha(alpha);
                three.changeAlpha(alpha);
            }
        });

        mCalendarDateView.setOnItemClickListener(new CalendarView.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, CalendarInfo bean) {
                mTitle.setText(bean.getYear() + "/" + bean.getMouth() + "/" + bean.getDay());
                view.findViewById(R.id.circle).setSelected(true);
            }
        });

        int[] data = CalendarUtil.getYMD(new Date());
        mTitle.setText(data[0] + "/" + data[1] + "/" + data[2]);
    }

    @OnClick(R.id.back)
    public void onClick() {
        linearLayout.requestLayout();
    }
}
